<?php

// wf_view.php
// Michael Baker
// 2010.07.14
//
// View workflow in detail

session_start();

define("IN_MDM", true);
define("CUR_PAGE_TITLE", "View workflow");
define("CUR_FLAG", "wf.view");

include '../includes/all.php';
include 'inc/header.php';

if ( !( array_key_exists( "id", $_GET ) && is_numeric( $_GET["id"] ) ) ) {
  echo ui_error( "No workflow ID supplied" );
  include 'inc/footer.php';
  die;
  
}

$id = $_GET["id"];

try {
  $wf = $dbase->get_workflow($id);
}
catch (DatabaseException $e) {
  echo ui_error( $e->getMessage() );
  include 'inc/footer.php';
  die;
}

$wf_title = $wf->get_title();
$children = $wf->get_children();

?>

<table class="type2p" cellspacing="1">
  <caption><?php echo "#$id: $wf_title"; ?></caption>
  <col class="col1" /><col class="col2" />
  
  <thead>
    <tr>
      <th>Property</th>
      <th>Value</th>
    </tr>
  </thead>
  
  <tbody>
    <tr>
      <td>Workflow ID:</td>
      <td><?php 
      
      echo $id . " [<a href=\"../status.php?wf=$id&tf=1\" target=\"_new\">click for 3-minute diagram</a>]"; 
      
      ?></td>
    </tr>
    
    <tr>
      <td>Workflow Title:</td>
      <td><?php echo $wf_title; ?></td>
    </tr>
    
    <tr>
      <td>Nodes Involved:</td>
      <td><?php 
      
      $segCount = count( $children );
      $childCount = $segCount + 1;
      
      echo "$childCount";
      
      ?></td>
    </tr>
  </tbody>
</table>

<br />

<table class="type2p" cellspacing="1">
  <caption>Workflow composition</caption>
  <col class="col2" /><col class="col2" /><col class="col2" />
  
  <thead>
    <tr>
      <th>Node Title</th>
      <th>Node Name</th>
      <th>Node Address</th>
    </tr>
  </thead>
  
  <tbody>
    <?php
      
      // This is pretty rough
      $has_n_view = has_flag( $uid, "n.view" );
      
      foreach ( $children as $nc ) {
        $ncc = $nc->get_nodes();
        
        $node = $ncc[ 0 ];
        $lnode = $ncc[ 1 ];
        
        $id = $node->get_id();
        $title = $node->get_title( 1 );
        $name = $node->get_title( 0 );
        $addr = $node->get_address();
        
        $lid = $lnode->get_id();
        $ltitle = $lnode->get_title( 1 );
        $lname = $lnode->get_title( 0 );
        $laddr = $lnode->get_address();
        
        echo "<tr>";
        
        $link1 = $has_n_view ? ui_create_link( $title, "n_view.php?id=$id" ) : $title;
        
        echo "<td>$link1</td>";
        echo "<td>$name</td>";
        echo "<td>$addr</td>";
        echo "</tr>";
        
      }
      
      $link1 = $has_n_view ? ui_create_link( $ltitle, "n_view.php?id=$lid" ) : $ltitle;
      
      echo "<tr>";
      echo "<td>$link1</td>";
      echo "<td>$lname</td>";
      echo "<td>$laddr</td>";
      echo "</tr>";
    ?>
  </tbody>
</table>

<?php include 'inc/footer.php'; ?>